/*
 * PurchaseManagement.fx
 * Author : Yu Min-ting
 * Created on 2009-7-24, 15:34:43
 */

package com.sjtu.fx.admin;

import javafx.scene.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import com.sjtu.fx.tools.*;

import com.sjtu.fx.tools.SwingTable.*;

import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.ext.swing.SwingComboBox;
import javafx.ext.swing.SwingComboBoxItem;

import javafx.scene.control.Hyperlink;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONObject;

import org.json.JSONArray;

import java.lang.System;

import com.sjtu.fx.user.Login;

import javafx.stage.Alert;

public class request{
    public var bookName:String;
    public var userName:String;
    public var requestDate:String;
    public var comment:String;
    public var score:Integer;
    public var isbn:String;
    public var author:String;
    public var publisher:String;
}


public class PurchaseManagement extends CustomNode {
    var panel:Panel;
    var requests:request[] = bind requests with inverse;
    public var selection:Integer;
    var purchaseDetails:PurchaseDetails;
    var requsetIdList:JSONArray;
    var inTable:SwingTable = SwingTable{
        columns: [
                    TableColumn {
                        text: "Book Name"
                    },
                    TableColumn {
                        text: "User Name"
                    },
                    TableColumn {
                        text: "Request Date"
                    },
                    TableColumn {
                        text: "Score"
                    },
                ]
        rows: bind for (u in requests)
                    TableRow{
                        cells:[
                            TableCell {
                                text:bind u.bookName
                            },
                            TableCell {
                                text:bind u.userName
                            },
                            TableCell {
                                text: bind u.requestDate
                            },
                            TableCell {
                                text: bind u.score.toString()
                            },
                        ]
                    }
        selection: bind selection with inverse

    }

    var statusText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Purchase Request Status: "
    };
    var statusBox = SwingComboBox {
            items: [
                SwingComboBoxItem {
                    text: "Submitted"
                    selected: true
                },
                SwingComboBoxItem {
                    text: "Accepted"
                },
                SwingComboBoxItem {
                    text: "Rejected"
                },
                SwingComboBoxItem {
                    text: "Undetermined"
                },
                SwingComboBoxItem {
                    text: "Solved"
                },

            ]
        }
    var statusButton = Button {
            text: "Search"
            action: function() {                
                    getRequestList(statusBox.selectedIndex);
            }
        }

   var statusSetText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Handle Purchase Request Status: "
    };
    var statusSetBox = SwingComboBox {
            items: [
                SwingComboBoxItem {
                    text: "Accepted"
                    selected:true
                },
                SwingComboBoxItem {
                    text: "Rejected"
                },
                SwingComboBoxItem {
                    text: "Undetermined"
                },
                SwingComboBoxItem {
                    text: "Solved"
                },

            ]
        }
    var statusSetButton = Button {
            text: "OK"
            action: function() {
                var reqId:Integer = requsetIdList.get(selection) as Integer;
                var userId:Integer = (this.parent.lookup("loginPanel") as Login).userid;
                    checkPurchaseRequest(reqId,userId,statusSetBox.selectedIndex+1);
                    delete requests[selection];
            }
        }
    var details = Hyperlink {
            text: "See Details>>"
            action: function() {
                purchaseDetails.visible = true;
                purchaseDetails.author = requests[selection].author;
                purchaseDetails.bookName = requests[selection].bookName;
                purchaseDetails.comment = requests[selection].comment;
                purchaseDetails.isbn = requests[selection].isbn;
                purchaseDetails.publisher = requests[selection].publisher;
                purchaseDetails.requestDate = requests[selection].requestDate;
                purchaseDetails.score = requests[selection].score;
                purchaseDetails.userName = requests[selection].userName;
            }
        }

     var alert = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9
     }
    
    override protected function create() : Node {

        Group{
            content:[
                 panel = Panel {
                    width:    430
                    height:   550
                    title:    "Purchase Book Management"
                    translateX: 0
                    translateY: 0
                },
               
                VBox{
                    spacing: 25
                    translateX:32 translateY: 60
                    content:[
                         HBox{
                            spacing:25
                            content:[statusText,statusBox,statusButton]
                        },
                        inTable,
                        HBox{
                            spacing:15
                            content:[statusSetText,statusSetBox,statusSetButton]
                        },
                        details,
                        
                    ]

                }
                purchaseDetails = PurchaseDetails{visible:false}
                alert
            ]
        }
     }

     function getRequestList(reqType:Integer){
         HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/PurchaserequetList"
       onOutput: function(os:OutputStream) {
            try {
                var index:Integer = requests.size()-1;
                while(index>=0){
                    requests[index] = null;
                    index--;
                }
                var json:JSONObject = JSONObject{};
                json.put("reqType",reqType);
    //            System.out.println(passWd);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                var json:JSONObject = JSONObject{};
                json = new JSONObject(result);
                var bookNameList:JSONArray = json.getJSONArray("bookNameList");
                var commentsList:JSONArray = json.getJSONArray("commentsList");
                var scoreList:JSONArray = json.getJSONArray("scoreList");
                var isbnList:JSONArray = json.getJSONArray("isbnList");
                var authorList:JSONArray = json.getJSONArray("authorList");
                var publisherList:JSONArray = json.getJSONArray("publisherList");
                var createDateList:JSONArray = json.getJSONArray("createDateList");
                var userNameList:JSONArray = json.getJSONArray("userNameList");
                requsetIdList = json.getJSONArray("requsetIdList");
                var i:Integer;
                while(i<bookNameList.length()){
                    var req:request = request{
                         bookName:bookNameList.get(i).toString()
                         userName:userNameList.get(i).toString()
                         requestDate:createDateList.get(i).toString()
                         comment:commentsList.get(i).toString()
                         score:scoreList.get(i) as Integer
                         isbn:isbnList.get(i).toString()
                         author:authorList.get(i).toString()
                         publisher:publisherList.get(i).toString()
                        };
                     i++;
                     requests[i] = req;
                }
                System.out.println("{bookNameList}");
            } finally {
                is.close();
            }
        }
    }.start();
     }

     function checkPurchaseRequest(reqId:Integer,userId:Integer,status:Integer){
        HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/CheckPurchaseRequest"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("requestId",reqId);
                json.put("userId", userId);
                json.put("status",status);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                alert.title = "Result";
                alert.content = "result";
                alert.visible = true;
                //Alert.inform(result);
            } finally {
                is.close();
            }
        }
    }.start();
     }

}
